/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.Brand;
import com.jeesite.modules.goods.entity.ZtBanner;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.service.BrandService;
import com.jeesite.modules.goods.service.ZtBannerService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.util.AliRequestUtils;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztBanner")
public class ZtBannerController extends BaseController {
	private String resultImg;
	private String imgName;
	
	@Autowired
	private ZtBannerService ztBannerService;
	@Autowired
	private ZtGoodsService ztGoodsService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtBanner get(String id, boolean isNewRecord) {

		ZtBanner ztBanner = new ZtBanner();
		ztBanner.setId(id);
		ztBanner = ztBannerService.get(ztBanner);
		if (ztBanner == null) {
			ztBanner = new ZtBanner();
		}
		return ztBanner;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztBanner:ztBannerData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtBanner ztBanner, Model model) {
	
		model.addAttribute("ztBanner", ztBanner);

		return "modules/ztBanner/ztBannerList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztBanner:ztBannerData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtBanner> listData(ZtBanner ztBanner,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if(ztBanner==null){
			ztBanner=new ZtBanner();
		}
		
		Page<ZtBanner> page = ztBannerService.findPage(new Page<ZtBanner>(
				request, response), ztBanner);

		
		
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztBanner:ztBannerData:view")
	@RequestMapping(value = "form")
	public String form(ZtBanner ztBanner, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("ztBanner", ztBanner);
		ZtGoods ztGoods = new ZtGoods();
		ztGoods.setGoodstemp("4");//
		List<ZtGoods> ztGoodsList = ztGoodsService.findList(ztGoods);
		model.addAttribute("ztGoodsList", ztGoodsList);
		Brand brand=new Brand();
		brand.setIsshow("1");
		List<Brand> brandidList = brandService.findList(brand);
		model.addAttribute("brandidList", brandidList);
		return "modules/ztBanner/ztBannerForm";
	}

	
	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztBanner:ztBannerData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtBanner ztBanner) {
		
		ztBannerService.save(ztBanner);
		imgName=ztBanner.getImgname();
		resultImg=ztBanner.getImg();
		if(StringUtils.isNotBlank(resultImg)&&!resultImg.startsWith("http")){
			resultImg=myProps.getServerPath()+resultImg;
		}
		
		 ExecutorService cachedThreadPool=Executors.newCachedThreadPool();
			cachedThreadPool.execute(new Runnable() {			
				@Override
				public void run() {
					// TODO Auto-generated method stub
					try{
						AliRequestUtils.uploadFile(resultImg, imgName,"system365");
					}catch(Exception ex){}
				}
		});
		
		return renderResult(Global.TRUE, text("保存轮播成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztBanner:ztBannerData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtBanner ztBanner) {
		ztBannerService.delete(ztBanner);
		return renderResult(Global.TRUE, text("删除Banner成功！"));
	}
}